package com.itany.ums;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        User u = new User();
        u.setName(req.getParameter("name"));
        u.setPwd(req.getParameter("pwd"));
        u.setAge(Integer.parseInt(req.getParameter("age")));
        u.setSex(req.getParameter("sex"));
        u.setHobby(String.join(",",req.getParameterValues("hobby")));
        u.setInfo(req.getParameter("info"));

        DBUtil util = new DBUtil();
        util.insert(u);

        resp.sendRedirect(req.getContextPath()+"/index"); // 跳转到另一个servlet
    }
}
